package com.tianqu.tms.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.dto.AucBidhistoryDto;
import com.tianqu.tms.dto.TmsFragileStickerHistoryDto;

public class TmsFragileStickerHistoryDaoImp extends TmsDao  implements TmsFragileStickerHistoryDao{

	protected TmsFragileStickerHistoryDaoImp() throws TmsDaoException {
		super();
	}

	@Override
	public int getFragileStickerByMemberId(String memberId) throws TmsDaoException{
		// 开始掉数据
		PreparedStatement ps = null;
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer("SELECT ");
		sql.append("COUNT(ID) AS fragileSticker ");
		sql.append("FROM ");
		sql.append(TmsTableNames.TMS_FRAGILESTICKER_HISTORY);
		sql.append(" WHERE  DATE_FORMAT(INSERTTIME, '%Y-%M') = DATE_FORMAT(NOW(), '%Y-%M') ");
		sql.append("   AND MEMBERID = ?");

	    int fragileSticker = 0;
		try {

			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, memberId);
			rs = ps.executeQuery();
			while (rs.next()) {
				fragileSticker = rs.getInt("fragileSticker");
			}
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());

		} finally {
			super.close(rs, ps);
		}
		return fragileSticker;
	}

 
	@Override
	public void insert(TmsFragileStickerHistoryDto dto) throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			StringBuffer sql = new StringBuffer("INSERT INTO ");
			sql.append(TmsTableNames.TMS_FRAGILESTICKER_HISTORY);
			sql.append("(");
			sql.append(" fragileStickerID, ");
			sql.append(" memberId, ");
			sql.append(" putType,  ");
			sql.append(" serial,  ");
			sql.append(" isPay,  ");
			sql.append(" insertTime,  ");
			sql.append(" insertUserId  ");
			sql.append("  ) VALUES ( ");
			sql.append(" ?,?,?,?,?,SYSDATE(),?)");
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, dto.getFragileStickerId());
			ps.setString(2, dto.getMemberId());
			ps.setString(3, dto.getPutType());
			ps.setString(4, dto.getSerial());
			ps.setString(5, dto.getIsPay());
			ps.setInt(6, dto.getInsertUserId());
			ps.executeUpdate();
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());

		} finally {
			super.close(rs, ps);
		}
		
	}
}
